import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common';
import { Response } from "express"
import { AaaException } from './AaaException';

//ExceptionFilter对象 - 抛出未处理异常
//@Catch(x) 指定处理的异常
//@UseFilters 将处理的异常应用到 handler 上

@Catch(AaaException)
export class AaaFilter implements ExceptionFilter {

  //(exception:异常对象 ,  host: 服务信息?)
  catch(exception: AaaException, host: ArgumentsHost) {

      if(host.getType() === 'http') {
        const ctx = host.switchToHttp();
        const response = ctx.getResponse<Response>();
        const request = ctx.getRequest<Request>();

        response
          .status(500)
          .json({
            aaa: exception.aaa,
          });
      } else if(host.getType() === 'ws') {
        const ctx = host.switchToWs();    //webSocket - 双向通讯协议,ws:// wss://开头,有状态的协议,这意味着客户端和服务器之间的连接将保持活动状态。


      } else if(host.getType() === 'rpc') {
        const ctx = host.switchToRpc;     //rpc - 远程过程调用
      }
  }
}
